<?php


namespace app\index\model;


use think\facade\Db;
use think\Model;
use think\model\Relation;

class Article extends Model {
    protected $json = ['file'];

    public function categoryName() {
        return $this->
        hasOne(Category::class, 'id', 'category_id')->
        visible(['title'])->bind(['title']);
    }

    public function getLastId($categoryId) {
        $lastId = $this->
        with('categoryName')->
        field(['id', 'category_id'])->
        where('category_id', $categoryId)->
        where('status', config('status.mysql.table_normal'))->
        order('update_time', 'desc')->
        limit(1)->
        find();
        return $lastId;
    }

    public function getFileAttr($value) {
        if (empty($value)) {
            return null;
        }
        foreach ($value as &$v) {
            $v['src'] = str_replace('\\', '/', $v['src']);
        }
        return stdClassToArray($value);
    }

    public function getArticleById($id) {
        $field = ['id', 'item_number', 'image_url', 'title', 'hyperlink', 'author', 'content', 'file', 'clicks', 'create_time'];
        $article = $this->
        where(['status' => config('status.mysql.table_normal')])->
        field($field)->
        find($id);
        return $article;
    }

    public function getBannerImages($limit = 5) {
        $field = ['id', 'image_url', 'hyperlink', 'is_jump'];
        $order = ['id' => 'desc'];
        $where = ['status' => config('status.mysql.table_normal'), 'is_banner' => 1];
        $banner = $this->
        field($field)->
        order($order)->
        where($where)->
        limit($limit)->
        select();
        return $banner;
    }

    public function getArticleListByCategoryId($categoryId, $num = 10, $keyword = '') {
        $field = ['id', 'item_number', 'title', 'is_jump', 'hyperlink', 'create_time'];
        $order = ['id' => 'desc'];
        $where = [
            'status' => config('status.mysql.table_normal'),
        ];
        if (!empty($categoryId)) {
            $where['category_id'] = $categoryId;
        }
        if (!empty($keyword)) {
            if (empty($categoryId)) {
                $list = $this->
                field($field)->
                order($order)->
                where('status', config('status.mysql.table_normal'))->
                whereLike('title|content|item_number', '%' . $keyword . '%')->
                paginate([
                    'list_rows' => $num,
                ]);
            } else {
                $findPid = Db::name('category')->where('pid', $categoryId)->field('id')->select();
                if (!$findPid->isEmpty()) {
                    $res = $findPid->each(function ($v) {
                        return $v['id'];
                    });
                    $where['category_id'] = $res->toArray();
                }
                $list = $this->
                field($field)->
                order($order)->
                where('status', config('status.mysql.table_normal'))->
                whereIn('category_id', $where['category_id'])->
                whereLike('title|content|item_number', '%' . $keyword . '%')->
                paginate([
                    'list_rows' => $num,
                ]);
            }
        } else {
            $list = $this->
            field($field)->
            order($order)->
            where($where)->
            paginate([
                'list_rows' => $num,
            ]);
        }
        $pages = $list->render();
        return [$list, $pages];
    }

    public function getArticleTitleByCategory1($categoryId, $categoryName, $num = 8) {
        $field = ['id', 'item_number', 'title',  'is_jump',  'hyperlink', 'clicks', 'create_time'];
        $order = ['id' => 'desc'];
        $where = ['status' => config('status.mysql.table_normal'), 'category_id' => $categoryId];
        $list = $this->
        field($field)->
        order($order)->
        where($where)->
        limit($num)->
        select();
        $list = $list->toArray();
        return [
            'category_id' => $categoryId,
            'category_name' => $categoryName,
            'data' => $list,
        ];
    }

    public function getArticleTitleByCategory($categoryId, $categoryName, $num = 8) {
        $field = ['id', 'title'];
        $where = ['status' => config('status.mysql.table_normal'), 'id' => $categoryId];
        $data = Category::where($where)->
        field($field)->
        with([
            'articles' => function ($query) use ($num) {
                $query->order('id', 'desc')->withLimit($num);
            },
            'child' => function ($query) use ($num) {
                $query->with(['articles' => function (Relation $q) use ($num) {
                    $q->order('id', 'desc')->withLimit($num);
                }]);
            }])->
        find();
        return $data;
    }
}